package com.lvtongka.app.dialog

import android.app.Dialog
import android.content.Context
import android.view.*
import androidx.recyclerview.widget.LinearLayoutManager
import com.lvtongka.app.R
import com.lvtongka.app.common.widget.SpacesItemDecoration
import com.lvtongka.app.databinding.DialogSelectCouponBinding
import com.lvtongka.app.dialog.adapter.CouponSelectAdapter
import com.lvtongka.app.dialog.adapter.CouponSelectQsAdapter
import com.lvtongka.app.dialog.adapter.DialogSelectListener
import com.lvtongka.app.dialog.adapter.OnItemSelectListener
import com.lvtongka.app.fragment.home.bean.TravelPassAssets
import com.lvtongka.app.fragment.home.bean.TravelPassAssetsData
import com.lvtongka.app.ui.coupon.bean.CouponItem

open class CouponSelectDialog(context: Context, theme: Int) : Dialog(context, theme) {
    private var binding: DialogSelectCouponBinding = DialogSelectCouponBinding.inflate(layoutInflater)
    private var adapter: CouponSelectAdapter? = null
    private var adapterQs: CouponSelectQsAdapter? = null
    private val list: MutableList<CouponItem> = mutableListOf()
    private val properties: MutableList<TravelPassAssets> = mutableListOf()

    init {
        window!!.decorView.setPadding(0, 0, 0, 0)
        window!!.setGravity(Gravity.RELATIVE_LAYOUT_DIRECTION or Gravity.BOTTOM)
        val lp = window!!.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = lp
        setContentView(binding.root)
        initView()
        initListener()
    }

    open fun hideDialog() {
        cancel()
        dismiss()
    }

    private fun initView() {
        binding.rvCoupon.layoutManager = LinearLayoutManager(context)
        binding.rvCoupon.addItemDecoration(SpacesItemDecoration(10))
        adapter = CouponSelectAdapter(object : DialogSelectListener {
            override fun onItemClick(couponItem: CouponItem) {
                mListener?.onClickConfirm(couponItem)
            }
        })
        binding.rvCoupon.adapter = adapter

        binding.rvCouponQs.layoutManager = LinearLayoutManager(context)
        binding.rvCouponQs.addItemDecoration(SpacesItemDecoration(10))
        adapterQs = CouponSelectQsAdapter(object : OnItemSelectListener {
            override fun onItemClick(passAssetsData : TravelPassAssets) {
                mListener?.onClickQs(passAssetsData)
            }
        })

        binding.rvCouponQs.adapter = adapterQs
    }

    fun setCouponList(itemList : MutableList<CouponItem>) {
        list.addAll(itemList)
        adapter?.setList(list)
    }

    fun setQsList(assetsData : TravelPassAssetsData) {
        properties.clear()
        properties.addAll(assetsData.properties)
        properties[assetsData.selected].isSelected = true
        adapterQs?.setList(properties)
    }

    private fun initListener() {
        binding.ivClose.setOnClickListener{
            hideDialog()
        }

        setOnKeyListener { _, keyCode, event ->
            false
        }

        binding.tvFlCoupon.setOnClickListener {
            binding.tvFlCoupon.setBackgroundResource(R.drawable.bg_dialog_coupon_select)
            binding.tvQsCard.setBackgroundResource(R.drawable.bg_dialog_coupon_select_un)

            binding.tvFlCoupon.setTextColor(binding.tvFlCoupon.resources.getColor(R.color.black))
            binding.tvQsCard.setTextColor(binding.tvQsCard.resources.getColor(R.color.text81848B))

            binding.rvCoupon.visibility = View.VISIBLE
            binding.rvCouponQs.visibility = View.GONE
            binding.tvEmpty.visibility = View.GONE
            if (list.isEmpty()) {
                binding.rvCoupon.visibility = View.GONE
                binding.tvEmpty.visibility = View.VISIBLE
            }
        }

        binding.tvQsCard.setOnClickListener {
            binding.tvFlCoupon.setBackgroundResource(R.drawable.bg_dialog_coupon_select_un)
            binding.tvQsCard.setBackgroundResource(R.drawable.bg_dialog_coupon_select)

            binding.tvFlCoupon.setTextColor(binding.tvQsCard.resources.getColor(R.color.text81848B))
            binding.tvQsCard.setTextColor(binding.tvQsCard.resources.getColor(R.color.black))

            binding.rvCoupon.visibility = View.GONE
            binding.rvCouponQs.visibility = View.VISIBLE
            binding.tvEmpty.visibility = View.GONE
            if (properties.isEmpty()) {
                binding.rvCouponQs.visibility = View.GONE
                binding.tvEmpty.visibility = View.VISIBLE
            }
        }
    }

    private var mListener: OnConfirmClickListener? = null

    interface OnConfirmClickListener {
        /**
         * 点击条目
         */
        fun onClickConfirm(itemBean: CouponItem?)

        fun onClickQs(passAssetsData : TravelPassAssets)
    }

    fun setOnItemClickListener(listener: OnConfirmClickListener) {
        this.mListener = listener
    }

}